-- MapSample

--[[ 说明：随机生成网格地图，地图比较简单，有植物和矿物两种资源，资源随机生成。
    植物有三种：松树、杨树、小草
    矿物有两种：铁矿、铜矿
    
    通过参数来控制网格数目、网格大小、资源生成几率
    控制参数比较简单，如下：
    gridCount：网格数目，范围：1~100，例如，设为3则生成3*3的地图，设为100，则生成100*100的地图。
    scaleX：单位网格大小比例，范围：1~100，该值越小，则单位网格越小；该值越大，则单位网格越大。
    scaleY：同上，若与scaleX相同则单位网格是正方形格子。
    plantSeed：植物生成几率，范围:大于4的数，该值越小，生成的植物越多；该值越大，生成的植物越少。
    minerialSeed：矿物生成几率，范围:大于3的数，该值越小，生成的矿物越多；该值越大，生成的矿物越少。
    
    几组参考值：
    
    3*3的地图
    gridCount = 3
    scaleX = 100
    scaleY = 100
    plantSeed = 20.0
    minerialSeed = 10.0
    
    5*5的地图
    gridCount = 5
    scaleX = 100
    scaleY = 100
    plantSeed = 20.0
    minerialSeed = 10.0
    
    14*14的地图，矿物中等
    gridCount = 14
    scaleX = 50
    scaleY = 50
    plantSeed = 20.0
    minerialSeed = 10.0
    
    14*14的地图，矿物稀少
    gridCount = 14
    scaleX = 50
    scaleY = 50
    plantSeed = 20.0
    minerialSeed = 50.0
    
    70*70的地图，矿物稀少
    gridCount = 70
    scaleX = 10
    scaleY = 10
    plantSeed = 20.0
    minerialSeed = 50.0
    
    200*200的缩微地图，矿物稀少
    gridCount = 200
    scaleX = 2
    scaleY = 2
    plantSeed = 20.0
    minerialSeed = 50.0
--]]


-- Use this function to perform your initial setup
function setup()
    print("Simple Map Sample!!")
    textMode(CORNER)
    spriteMode(CORNER)
    
    gridCount = 5
    
    
    scaleX = 50
    scaleY = 50
    -- number > 4
    plantSeed = 20.0
    -- number > 3
    minerialSeed = 50.0
    
    --[[
    parameter.integer("scaleX",20,100,50)
    parameter.integer("scaleY",20,100,50)
    parameter.number("plantSeed",5.0,100.0,10.0)
    parameter.number("minerialSeed",4.0,100.0,10.0,resetMapTable)
    --parameter.integer("gridCount",1,10,6)
    --]]
    
    imgMap = image((gridCount+1)*scaleX,(gridCount+1)*scaleY)
    
    mapT = {}
    
    tree1 = "松树"
    tree2 = "杨树"
    tree3 = "小草"
    mine1 = "铁矿"
    mine2 = "铜矿"
    
    imgTree1 = "Planet Cute:Tree Short"
    imgTree2 = "Planet Cute:Tree Tall"
    imgTree3 = "Platformer Art:Grass"
    imgMine1 = "Platformer Art:Mushroom"
    imgMine2 = "Small World:Treasure"
    
    itemTable = {[tree1]=imgTree1,[tree2]=imgTree2,[tree3]=imgTree3,[mine1]=imgMine1,[mine2]=imgMine2}
    
    
    -- 3*3 
    mapTable = {{pos=vec2(1,1),plant=nil,mineral=mine1},{pos=vec2(1,2),plant=nil,mineral=nil},{pos=vec2(1,3),plant=tree3,mineral=nil},
                {pos=vec2(2,1),plant=tree1,mineral=nil},{pos=vec2(2,2),plant=tree2,mineral=mine2},{pos=vec2(2,3),plant=nil,mineral=nil},
                {pos=vec2(3,1),plant=nil,mineral=nil},{pos=vec2(3,2),plant=nil,mineral=mine2},{pos=vec2(3,3),plant=tree3,mineral=nil}}

    --print(randomPlant())
    --print(randomMinerial())

    --mapTable = createMap()
    --mapTable = {}
    mapTable = createMap()
end

function resetMapTable()
    mapTable = createMap()
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    
    
    
    --if #mapTable == 0 then mapTable = createMap() end

    -- This sets the line thickness
    -- strokeWidth(5)

    -- Do your drawing here

    sImgMap = drawMap()
    sprite(sImgMap,0,0)
end

function createMap()
    for i=1,gridCount,1 do
        for j=1,gridCount,1 do
            mapItem = {pos=vec2(i,j),plant=randomPlant(),mineral=randomMinerial()}
            table.insert(mapT,mapItem)
            --print(unpack(mapItem))
            --print(mapItem[plant],mapItem[mineral])
        end
    end
    return mapT
end

function randomPlant()
    local seed = math.random(1.0,plantSeed)
    local result = nil
    if seed >= 1 and seed < 2 then result = tree1 end
    if seed >= 2 and seed < 3 then result = tree2 end
    if seed >= 3 and seed < 4 then result = tree3 end
    if seed >= 4 and seed <= plantSeed then result = nil end
    
    return result
end

function randomMinerial()
    local seed = math.random(1.0,minerialSeed)
    local result = nil

    if seed >= 1 and seed < 2 then result = mine1 end
    if seed >= 2 and seed < 3 then result = mine2 end
    if seed >= 3 and seed <= minerialSeed then result = nil end
    
    return result

end

function getImg(name)
    return itemTable[name]
end

function drawMap()
    
    setContext(imgMap)
    
    for i = 1,gridCount*gridCount,1 do
        drawGround(mapTable[i].pos)
        if mapTable[i].plant ~= nil then drawTree(mapTable[i].pos,mapTable[i].plant) end
        if mapTable[i].mineral ~= nil then drawMineral(mapTable[i].pos,mapTable[i].mineral) end
    end
    
    setContext()
    return imgMap
end

function drawGround(position)
    local x,y = scaleX * position.x, scaleY * position.y
    strokeWidth(1)
    fill(5,155,40,255)
    fill(5,155,140,255)
    rect(x,y,scaleX,scaleY)
end

function drawTree(position,plant)
    local x,y = scaleX * position.x, scaleY * position.y
    
    sprite(itemTable[plant],x,y,scaleX*4/10,scaleY)
    
    fill(100,100,200,255)
    --text(plant,x,y)

end

function drawMineral(position,mineral)
    local x,y = scaleX * position.x, scaleY * position.y
    
    sprite(itemTable[mineral],x+scaleX/2,y,scaleX/2,scaleX/2)

    fill(100,100,200,255)
    text(mineral,x+scaleX/2,y)
    
end
